/*
 ============================================================================
 Name		: ImageEncoder.cpp
 Author	  : Lenka Litvova
 Version	 : 1.0
 Copyright   : Copyright belongs to Lenka Litvova
 Description : CImageEncoder implementation
 ============================================================================
 */

#include <ImageConversion.h> 
#include <BAUTILS.H>
#include <eikenv.h>
#include "ImageSaver.h"
#include "Globals.h"
#include "FRamI_UIAppUi.h"

const TInt KFileNameIndexMaxLength = 5;
_LIT(KImageFileExtension,".jpg");
_LIT(KImageFileName, "User");
_LIT8(KMimeType,"image/jpeg");

CImageSaver::CImageSaver(CFRamI_UIAppUi *aAppUi) :iAppUi(aAppUi),
	CActive(EPriorityStandard) // Standard priority
	{
	}

CImageSaver* CImageSaver::NewLC(CFRamI_UIAppUi *aAppUi)
	{
	CImageSaver* self = new (ELeave) CImageSaver(aAppUi);
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
	}

CImageSaver* CImageSaver::NewL(CFRamI_UIAppUi *aAppUi)
	{
	CImageSaver* self = CImageSaver::NewLC(aAppUi);
	CleanupStack::Pop(); // self;
	return self;
	}

void CImageSaver::ConstructL()
	{
	iFs.Connect();
	iSaveBmp=new (ELeave) CFbsBitmap;
	
	TFileName path (KUsers); 
	iImagePath = HBufC::NewL( path.Length() );
	iImagePath->Des().Copy( path);
	
	CActiveScheduler::Add(this); // Add to scheduler
	}

CImageSaver::~CImageSaver()
	{
	Cancel(); // Cancel any request, if outstanding
	
	// Delete instance variables if any
	if(iEncoder)
	{
		delete iEncoder;
		iEncoder=NULL;			
	}
	
	delete iSaveBmp;
	iSaveBmp=NULL;
	}

void CImageSaver::DoCancel()
	{
		iEncoder->Cancel();
	}

void CImageSaver::RunL()
	{
		if(iStatus==KErrNone)
			{
			iAppUi->SetLastSavedImage(iNewFileName);			
			}
	}

TInt CImageSaver::RunError(TInt aError)
	{
	return aError;
	}

void CImageSaver::SaveImage(const CFbsBitmap& aBitmap)
	{
	if(iEncoder)
		{
		delete iEncoder;
		iEncoder=NULL;
		}
	GetNextUsableFileName();
	
	RDebug::Print(iNewFileName);
	
	iEncoder=CImageEncoder::FileNewL(iFs,iNewFileName,KMimeType);
	iSaveBmp->Duplicate(aBitmap.Handle());
	iEncoder->Convert(&iStatus,*iSaveBmp);
	SetActive();
	}

void CImageSaver::SaveImage(const CFbsBitmap* aBitmap)
	{
	if(iEncoder)
		{
		delete iEncoder;
		iEncoder=NULL;
		}
	GetNextUsableFileName();
	
	RDebug::Print(iNewFileName);
	
	iEncoder=CImageEncoder::FileNewL(iFs,iNewFileName,KMimeType);
	iSaveBmp->Duplicate(aBitmap->Handle());
	iEncoder->Convert(&iStatus,*iSaveBmp);
	SetActive();
	}

void CImageSaver::GetNextUsableFileName()
    {	
    TInt index = 0;

    do {
        iNewFileName.Copy( iImagePath->Des() );
        TBuf<KFileNameIndexMaxLength> num;
        num.Num( index );
     
	   iNewFileName.Append( KImageFileName );
	   iNewFileName.Append( num );	
	   iNewFileName.Append( KImageFileExtension );
	   
	   if ( !BaflUtils::FileExists( CEikonEnv::Static()->FsSession(),iNewFileName ) )
            break;

        index ++;
        } while ( 1 );
    }
